<template>
  <div style="display: none">
    <div class="slot-component" :class="className" v-if="sidebarItems.length">
      <ul class="nav">
        <li
          v-for="item of sidebarItems"
          :key="item.name"
          :title="item.title"
          :class="activeNav === item.name ? 'active' : ''"
          @click="changeActiveNav(item.name)"
        >
          <i :class="'iconfont icon-' + item.icon"></i>
        </li>
      </ul>
      <div class="content">
        <component :is="activeNav" />
      </div>
      <div class="collapse" @click="toggleCollapse">
        <i :class="iconClass"></i>
      </div>
    </div>
  </div>
</template>

<script>
import Comment from './Comment.vue'
import CheckResult from './CheckResult.vue'
import Download from './Download.vue'
import Outline from './Outline.vue'
import Versions from './Versions.vue'
import Keyword from './Keyword.vue'
import Variable from './Variable.vue'
export default {
  props: {
    defaultActiveNav: { type: String, default: '' },
    // defaultCollapse: { type: Boolean, default: false },
    sidebarItems: { type: Array, default: () => [] },
    side: { type: String, default: 'left' } // left || right
  },
  components: {
    Outline,
    Versions,
    Comment,
    CheckResult,
    Keyword,
    Download,
    Variable
  },
  computed: {
    className() {
      const cls = this.side === 'left' ? 'left-sidebar' : 'right-sidebar'
      return this.collapse ? cls + ' collapse' : cls
    },
    collapse() {
      return this.$store.state.xmleditor.sidebar[this.side].collapse
    },
    activeNav() {
      return this.$store.state.xmleditor.sidebar[this.side].activeNav
    },
    iconClass() {
      if (this.side === 'left') {
        return this.collapse ? 'el-icon-arrow-right' : 'el-icon-arrow-left'
      }
      return this.collapse ? 'el-icon-arrow-left' : 'el-icon-arrow-right'
    }
  },
  methods: {
    changeActiveNav(activeNav) {
      // 1. 折叠状态下
      if (this.collapse) {
        this.toggleCollapse()
      } else {
        // 2. 打开
        if (activeNav === this.activeNav) {
          this.toggleCollapse()
        }
      }
      this.$store.commit('xmleditor/CHANGE_SIDEBAR_ACTIVE_NAV', { side: this.side, activeNav })
    },
    toggleCollapse() {
      this.$store.commit('xmleditor/CHANGE_SIDEBAR_ACTIVE_COLLAPSE', { side: this.side, collapse: !this.collapse })
    }
  },
  mounted() {
    this.$store.commit('xmleditor/CHANGE_SIDEBAR_ACTIVE_NAV', { side: this.side, activeNav: this.defaultActiveNav })
  }
}
</script>
<style scoped>
@import url('./iconfont/iconfont.css');

.slot-component.collapse {
  width: 40px;
}
.slot-component {
  width: 350px;
  position: relative;
  transition: all 0.25s;
  display: flex;
  background-color: #f3f3f3;
  border-right: 1px solid #ccc;
}

.slot-component ul.nav {
  list-style-type: none;
  border-right: 1px solid #ccc;
}

.slot-component ul.nav > li {
  width: 40px;
  font-size: 12px;
  padding: 7px 0;
  text-align: center;
  cursor: pointer;
}

.slot-component ul.nav > li > i {
  padding: 3px 5px;
  margin-left: 1px;
  border-radius: 3px;
  color: rgba(0, 0, 0, 0.6);
  font-size: 20px;
}

.slot-component ul.nav > li.active > i {
  background-color: rgba(0, 0, 0, 0.5);
  color: #fff;
  border-top-left-radius: 3px;
  border-bottom-left-radius: 3px;
}

.slot-component .content {
  flex: 1;
  min-width: calc(100% - 41px);
  background-image: linear-gradient(0deg, #eee, #fff);
  position: relative;
  transition: width 0.5s;
}
.slot-component .content.content-collapse {
  flex: 0;
  width: 0;
  min-width: 0;
}

.slot-component .content > div {
  width: 100%;
  height: 100%;
  position: relative;
  overflow: auto;
  background-color: #fff;
}

.slot-component .content > div .refresh-icon {
  position: absolute;
  top: 5px;
  right: 5px;
  cursor: pointer;
}

.slot-component .content > div .loading-page {
  position: absolute;
  top: 70px;
  right: -38px;
  font-size: smaller;
  transform: rotate(-90deg);
  color: #888;
}

.slot-component .content > div.draft-content {
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.slot-component .content > div.draft-content .icon-Check {
  color: #409eff;
}

.slot-component .content > div > h3 {
  font-size: 14px;
  padding: 5px 10px;
  border-bottom: 1px solid #ddd;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.slot-component .content > div ul {
  list-style-type: none;
  width: 100%;
  min-height: 400px;
  flex: 1;
  overflow: auto;
}

.slot-component .content > div ul.imgs > li {
  text-align: center;
  margin: 15px 0;
  position: relative;
}

.slot-component .content > div ul.imgs > li > img {
  width: 150px;
  border: 1px solid #ddd;
}

.slot-component .content > div ul.imgs > li.disabled:before {
  content: '\e6e5';
  font-family: element-icons !important;
  transform: rotate(-45deg);
  position: absolute;
  top: calc(50% - 60px);
  font-size: 5rem;
  z-index: 10;
  right: calc(50% - 35px);
  color: rgba(0, 0, 0, 0.05);
  font-weight: 700;
}

.slot-component .content > div ul.draft-list {
  padding: 10px 15px;
}

.slot-component .content > div ul.draft-list > li {
  display: flex;
  font-size: 12px;
  align-items: center;
  justify-content: space-between;
  padding: 6px 0;
  border-bottom: 1px solid #eee;
  color: #666;
}

.slot-component .content > div ul.draft-list > li .iconfont {
  font-size: 1.25em;
}

.slot-component .content > div ul.draft-list > li span {
  margin-left: 5px;
  cursor: pointer;
}

.slot-component .content > div ul.draft-list > li .bytes {
  color: #888;
  font-size: smaller;
}

.slot-component .content > div ul.draft-list > li:hover {
  color: #409eff;
}

.slot-component .content > div ul.file-list {
  padding: 10px;
}

.slot-component .content > div ul.file-list > li {
  border-bottom: 1px solid #ddd;
  padding: 5px 0;
  font-size: 12px;
}

.slot-component .content > div ul.file-list > li .title {
  display: flex;
}

.slot-component .content > div ul.file-list > li .title > i {
  color: #888;
  margin-right: 5px;
  font-size: 14px;
}

.slot-component .content > div ul.file-list > li .title > span {
  font-weight: 700;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.slot-component .content > div ul.file-list > li .desc {
  color: #666;
  display: flex;
  justify-content: space-between;
}

.slot-component .content > div ul.file-list > li .desc button {
  margin: 0 2px;
  cursor: pointer;
}

.slot-component .collapse {
  padding: 3px 0;
  cursor: pointer;
  position: absolute;
  left: 14px;
  bottom: 20px;
  z-index: 100;
  border-top-right-radius: 3px;
  border-bottom-right-radius: 3px;
  width: 12px;
  text-align: center;
  height: 18px;
  display: flex;
}

.slot-component .collapse > i {
  font-size: 20px;
}
</style>
